package hw.ats.news.activity.etc;

import hw.ats.common.model.Config;
import hw.ats.news.R;
import hw.ats.news.activity.DBAwareActivity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.humanwares.util.StringUtil;

public class RecmdAppAct extends DBAwareActivity {
	@SuppressWarnings("unused")
	private final static String TAG = "RecmdAppAct";
	private ListView cntView = null;
	private List<String> cntItems = null;
	private Config config = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_recmd_app);
		w.setLayout(
				ViewGroup.LayoutParams.FILL_PARENT, 
				ViewGroup.LayoutParams.WRAP_CONTENT);
		w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				android.R.drawable.ic_dialog_info);
		
		config = Config.load(getReadableDatabase());
		if (config == null) {
			Toast.makeText(this, "앱스팩토리의 앱 정보를 조회할 수 없습니다.", Toast.LENGTH_SHORT).show();
			return;
		}
		
        cntItems = new ArrayList<String>();
        cntItems.add(getString(R.string.recmd_app_email));
        cntItems.add(getString(R.string.recmd_app_sms));
        cntItems.add(getString(R.string.recmd_app_cancel));
        
        cntView = (ListView)findViewById(R.id.cnt_view);
        cntView.setAdapter(new RecmdAppAdapter(this));
        cntView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 이메일 추천
				if (position == 0) {
					String body = StringUtil.nvl(config.memo).replaceAll("<[^>]+>", "");
					Intent sendIntent = new Intent(Intent.ACTION_SEND);
					sendIntent.setType("message/rfc822");
					sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[0]);  
					sendIntent.putExtra(Intent.EXTRA_SUBJECT, StringUtil.nvl(config.title) + " 앱을 추천합니다.");  
					sendIntent.putExtra(Intent.EXTRA_TEXT, body + "\n주소: "+StringUtil.nvl(config.url));  
					startActivity(Intent.createChooser(sendIntent, getString(R.string.recmd_app_email)));   
				}
				
				// SMS 추천
				else if (position == 1) {
					Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:"));
					sendIntent.putExtra("sms_body", StringUtil.nvl(config.title) + " 앱을 추천합니다.\n주소: " + StringUtil.nvl(config.url));
					startActivity(Intent.createChooser(sendIntent, getString(R.string.recmd_app_sms)));
				}
				
				// 취소
				else {
					finish();
				}
			}
		});
        
    }

	private class RecmdAppAdapter extends BaseAdapter {
		@SuppressWarnings("unused")
		private final static String TAG = "RecmdAppAdapter";
	   	private final Activity context;

        public RecmdAppAdapter(Activity context) {
        	this.context = context;
        }

        public int getCount() {
        	if (cntItems == null)
        		return 0;
        	
        	return cntItems.size();
        }

        public Object getItem(int position) {
            return cntItems.get(position);
        }

        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)context.getSystemService(
                		Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row_recmdapp, null);
            }
            
            String title = cntItems.get(position);
            
            TextView textView = (TextView)v.findViewById(R.id.row_title);
            textView.setText(title);
            
            return v;
        }
    }
}
